# view_db.py
import sqlite3
import sys

def view_database(db_path):
    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 获取所有表名
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
        tables = cursor.fetchall()
        
        print(f"📊 数据库: {db_path}")
        print(f"📋 表列表: {[table[0] for table in tables]}\n")
        
        for table in tables:
            table_name = table[0]
            print(f"=== 表: {table_name} ===")
            
            # 获取表结构
            cursor.execute(f"PRAGMA table_info({table_name})")
            columns = cursor.fetchall()
            print("列名:", [col[1] for col in columns])
            
            # 获取前10条数据
            cursor.execute(f"SELECT * FROM {table_name} LIMIT 10")
            rows = cursor.fetchall()
            
            if rows:
                for i, row in enumerate(rows, 1):
                    print(f"{i}. {row}")
            else:
                print("  (空表)")
            print()
        
        conn.close()
        
    except Exception as e:
        print(f"❌ 错误: {e}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("用法: python view_db.py <数据库文件路径>")
        sys.exit(1)
    
    db_file = sys.argv[1]
    view_database(db_file)